Database reference guide |
HOME |
Conditional ClausesThe IF() statement allows expressions to be defined according to conditions: IF (<condition>, <true case>, <false case> ) When <condition> is true, the clause takes on the value of the <true case> or else it takes on the value of the <false case>. Consider: IF( Age >= 65 , “RETIRED” , “NOT RETIRED”) For each row in the table, this will return either "RETIRED" or "NOT RETIRED" depending on the age of the customer in each record. This is an over simplification, since women usually retire earlier than men, we can deal with this problem as follows: IF( (Sex=”F”) and (Age >=60 ) , “RETIRED” , IF( (Sex=”M”) and ( Age >=65 ) , “RETIRED”,”NOT RETIRED”)) The first IF's condition checks if we have a retired female, if so then we return "RETIRED" else we perform another IF to check for retired males, only if this is false do we then return "NOT RETIRED".
The IF(..) statement can alternatively be expressed in a potentially more readable format:
So that the previous statement could be written: IF( Income < 5000 ) THEN 0 ELSE IF( Income < 10000) THEN 2 ELSE IF( Income < 15000) THEN 3 ELSE IF( Income < 20000 THEN 4 ELSE 5 ENDIF ENDIF ENDIF ENDIF In addition to the IF clause, a multiple simultaneous comparison is possible using the SWITCH clause of the basic form: SWITCH( <value> ) CASE <value1> : <assigned 1> , CASE <value2> : < assigned 2> , CASE <value3> : < assigned 3> , ELSE : <condition> SWITCH END The contents of <value> are compared in turn with each of the values in the CASE clauses. When it finds one that matches, it returns the result assigned to that case. If none match, then it returns the result assigned to the ELSE clause.
For example: SWITCH (Gender)
CASE “F”: “Female”, ELSE: “Unknown” SWITCH END
The ELSE clause is optional. If omitted, any records that do not match a case will be assigned a Null value. Conversely, if a record matches more than one case, it will be assigned the value from the first matching case. SWITCH (Income)
SWITCH END
It is also possible to perform simultaneous multiple comparisons using the CASE clause which can take two forms. The basic form is: CASE <expression> WHEN <value1> THEN <assigned1> WHEN <value2> THEN <assigned2> ... ELSE <assigned> END The contents of <value> is compared in turn with each of the values in the WHEN clauses. When it finds one that matches, it returns the result assigned to that case. If none match, then it returns the result assigned to the optional ELSE clause. For example:
The other form of the CASE clause allows for a number of defined conditions to be tested. The syntax for this form is:
Each <condition> is tested in turn. When a condition is met then the value is returned. If none of the conditions are met then it returns the result assigned to the optional ELSE clause.
An example of this form is: CASE WHEN [demo].[person].[Income] > 100 THEN "High" WHEN [demo].[person].[Income] > 50 THEN "Middle" ELSE "Low" END The DISTINCT expression can be used to return a distinct number for each distinct value. For example: Select expr{distinct(gender)}, gender from person; Returns: M 2 F 1 M 2 F 1 F 1 F 1 M 2 |
Online & Instructor-Led Courses | Training Videos | Webinar Recordings | ![]() |
|
![]() |
© Alterian. All Rights Reserved. | Privacy Policy | Legal Notice | ![]() ![]() ![]() |